 <?php
class Jqueryplugins extends Model_Abstract
{
	public $tableName = 'jqueryplugins';
	
	public $descriptionVars = array(
		'[[a_rf]]' => '当前编辑插件生成的附件目录 ',
		'<*' => '&lt;',
		'*>' => '&gt;'
	);
	
	public function descriptionVarsTrans($attributes = null)
	{
		$attributes = $attributes? $attributes : $this->attributes;
		return array(
			'[[a_rf]]' => FRONTEND_RESOURCEROOT . $attributes['root_foldername'] .'/',
			'<*' => '&lt;',
			'*>' => '&gt;'
		);
	}
	
	protected $_validation = array(
		'name' => array(
			array('max_length', '模板名称长度不能超过100', 100),
			array('not_empty', '请填写模板名称')
		),
		'root_foldername' => array(
			array('max_length', '资源路径最长100个字符', 100),
		),
		'description' => array(
			array('max_length', '请在500个字以内描述模板', 500),
		),
		'thumbnail' => array(
			array('max_length', '预览图名称最长50个字符', 50),
			array('not_empty', '请放置一张预览图')
		),
		'download_pack' => array(
			array('max_length', '下载包文件名最长50个字符', 50),
		),
		'view_num' => array(
			array('is_int', 'view_num必须是数字'),
		),
	);
	
	public function init()
	{
		$this->initAttributes();
		$this->_notFilterScriptTagsAttributes = array('description');
	}
	
	public static function model($classname = __CLASS__)
	{
		return parent::model($classname);
	}
	
	public function attributeLabels()
	{
	}
	
	public function getAll()
	{
		$sql = " SELECT 
					jq.*, 
					jq_t.name type, jq_t.name_en type_en 
				FROM 
					jqueryplugins jq 
				LEFT JOIN 
					jqueryplugins_type jq_t ON jq.type_id = jq_t.id ";

		return $this->_connection->query($sql);
	}
	
	public function initAttributes()
	{
		$this->attributes = array(
				'name' => '',
				'description' =>'',
				'root_foldername' => '',
				'thumbnail' => '',
				'download_pack' => '',
				'view_num' => '',
				'type_area_id' => '',
				'created_date' => '',
				'created_int' => ''
		);
	}
	
	public function replaceSelfDescriptionVars()
	{
		if(!empty($this->attributes['description'])){
			foreach($this->descriptionVarsTrans() as $from => $to){
				$this->attributes['description'] = str_replace($from, $to, $this->attributes['description']);
			}
		}	
	}
	
	public function replaceDescriptionVars($attributes)
	{
		if(!empty($attributes['description'])){
			foreach($this->descriptionVarsTrans($attributes) as $from => $to){
				$attributes['description'] = str_replace($from, $to, $attributes['description']);
			}
		}
		
		return $attributes;
	}
	
	protected function _find($fields = '*', $attributes = array(), $conditions = array(), $params = array(), $constraint = array())
	{
		$result = parent::_find($fields, $attributes, $conditions, $params, $constraint);
		$c = array_merge(
				array('groupBy' => array(), 'onlyOne' => false, 'distinct' => false, 'stripName' => false, 'setResultKey' => false, 'replaceDescriptionVars' => false),
				$constraint
		);
		extract($c);
		
		if($replaceDescriptionVars){
			if($onlyOne){
				$result = $this->replaceDescriptionVars($result);
			}else{
				foreach($result as $k => $r){
					$result[$k] = $this->replaceDescriptionVars($r);
				}
			}
		}
		
		return $result;
	}
	
	protected function _save($validate = true)
	{
		if(!empty($this->attributes[$this->primaryKey])){
			$oldData = $this->_find('*', array('id' => $this->attributes['id']), null, null, array('onlyOne' => true));
			
			if($oldData['name'] != $this->attributes['name']){
				if($this->count(array('name' => $this->attributes['name'])) > 0){
					$this->_validationError['name'][] = 'name' . '已存在';
					return false;
				}
			}
			if($oldData['root_foldername'] != $this->attributes['root_foldername']){
				if($this->count(array('root_foldername' => $this->attributes['root_foldername'])) > 0){
					$this->_validationError['root_foldername'][] = 'root_foldername' . '已存在';
					return false;
				}
				
				rename(
					JQUERYPLUGINS_DIR . '/' . $oldData['root_foldername'],
					JQUERYPLUGINS_DIR . '/' . $this->attributes['root_foldername']
				);
			}
		}
		
		$this->_before_sql();
		return parent::_save($validate);
	}

	public function saveFiles($fileInfos)
	{
		$root = JQUERYPLUGINS_DIR . '/' . $this->attributes['root_foldername'];
		@mkdir($root, 0777, true);
		
		if(!empty($fileInfos['name']['attachment'])){
			$attachments = $fileInfos['name']['attachment'];
			$tmpName = $fileInfos['tmp_name']['attachment'];
			if(!is_array($attachments)){
				$attachments = array($attachments);
			}
			if(!is_array($tmpName)){
				$tmpName = array($tmpName);
			}
			
			foreach($attachments as $k => $attach){
				if(!empty($attach)){
					copy($tmpName[$k], $root . '/' . $attach);
				}
			}
		}
		
		if(!empty($fileInfos['name']['thumbnail'])){
			$thumbnail = $this->attributes['thumbnail'];
			$tmpName = $fileInfos['tmp_name']['thumbnail'];
			
			copy($tmpName, $root . '/' . $thumbnail);
		}
		
		if(!empty($fileInfos['name']['download_pack'])){
			$download_pack = $this->attributes['download_pack'];
			$tmpName = $fileInfos['tmp_name']['download_pack'];
			
			copy($tmpName, $root . '/' . $download_pack);
		}
	}
	
	protected function _delete($deleteFiles = false, $attributes = array(), $conditions = array(), $params = array())
	{
		if($deleteFiles){
			$root = $this->find('root_foldername', $attributes, $conditions, $params, array('onlyOne' => true, 'stripName' => true));
			$root = JQUERYPLUGINS_DIR . '/' . $root;
			@mkdir($root, 0777, true);
			Util::removePath($root);
		}
		$this->_before_sql();
		parent::_delete($attributes, $conditions, $params);
	}
	
}